package com.teacher.service.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.teacher.pojo.entity.BedroomLevel;
import com.teacher.pojo.entity.PracticeGrades;
import com.teacher.pojo.enums.BedroomLevelType;
import com.teacher.pojo.excel.BedroomLevelExcel;
import com.teacher.pojo.excel.PracticeGradesExcel;
import com.teacher.service.IBedroomLevelService;
import com.teacher.service.IPerStudentService;
import com.teacher.service.IPracticeGradesService;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zfm
 * @Date: 2021/7/30 16:17
 */
public class ExcelPracticeGradesListener extends AnalysisEventListener<PracticeGradesExcel> {
    private Long termId;
    private IPracticeGradesService iPracticeGradesService;

    private List<PracticeGrades> practiceGradesList = new ArrayList<>();
    private IPerStudentService iPerStudentService;

    public ExcelPracticeGradesListener(Long termId, IPracticeGradesService iPracticeGradesService, IPerStudentService iPerStudentService) {
        this.termId = termId;
        this.iPracticeGradesService = iPracticeGradesService;
        this.iPerStudentService = iPerStudentService;
    }

    @Override
    public void invoke(PracticeGradesExcel practiceGradesExcel, AnalysisContext analysisContext) {
        if (iPerStudentService.isExit(practiceGradesExcel.getStudentKey())) {
            PracticeGrades practiceGrades = new PracticeGrades();
            BeanUtils.copyProperties(practiceGradesExcel, practiceGrades);
            practiceGrades.setTermId(termId);
            practiceGradesList.add(practiceGrades);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }

    private void saveData() {
        iPracticeGradesService.saveBatch(practiceGradesList);
    }
}
